package com.proba.annotations.hibernate;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;

import com.proba.annotations.PartnerDAO;
import com.proba.annotations.pojo.Partner;

public class PartnerHibernateDAO extends
		AbstractHibernateDAO<Partner, Long> implements
		PartnerDAO {

	public List<Partner> findByPartner(String partner) {
		return findByCriteria(Restrictions.eq("partner", partner));
	}
	
	public List<Partner> findByTipPartner(Long tipPartner) {
		return findByCriteria(Restrictions.eq("tipPartner.tipPartner", tipPartner));
	}
	
	public List<Partner> findBySluzba(String sluzba) {
		return findByCriteria(Restrictions.eq("sluzba", sluzba));
	}

	@Override
	public List<Partner> findByFilterParams(int startIndex, int pageSize,
			Long id, String partner, String sluzba, String tehnickiBroj,
			String tipPartner) {
		Criteria crit = getCriteria(startIndex, pageSize);
		if (id != null) {
			crit.add(Restrictions.eq("id", id));
			return crit.list();
		}
		if (notEmpty(partner)) {
			crit.add(like("partner", partner));
		}
		if (notEmpty(sluzba)) {
			crit.createCriteria("sluzba").add(like("sluzba", sluzba));
		}
		if (notEmpty(tehnickiBroj)) {
			crit.add(like("tehnickiBroj", tehnickiBroj));
		}
		if (notEmpty(tipPartner)) {
			crit.createCriteria("tipPartner").add(like("tipPartner", tipPartner));
		}
		return crit.list();
	}

}
